import plugin from '../../../lib/plugins/plugin.js'
import { segment } from "oicq"
import fs from "fs"
import data from '../model/XiuxianData.js'
import { timestampToTime, shijianc, get_random_fromARR, ForwardMsg } from './xiuxian.js'

//要DIY的话，确保这两个数组长度相等
const 宗门人数上限 = [6, 9, 12, 15, 18, 21];
const 长老人数上限 = [1, 2, 3, 4, 5, 7];
const 内门弟子上限 = [2, 3, 4, 5, 6, 8];
const 宗门灵石池上限 = [200000, 500000, 800000, 1100000, 1500000, 2000000];
const 出入宗门CD = 6;//单位小时
//处理消息
export class association extends plugin {
    constructor() {
        super({
            /** 功能名称 */
            name: 'Yunzai_Bot_修仙宗门v3.0',
            /** 功能描述 */
            dsc: '宗门模块',
            event: 'message',
            /** 优先级，数字越小等级越高 */
            priority: 600,
            rule: [
                {
                    //开发用的,记得删
                    reg: '^#宗门测试$',
                    fnc: 'zongmen_test'
                },
                {
                    reg: '^#开宗立派$',
                    fnc: 'Create_association'
                },
                {
                    //之后搞个图
                    reg: '^#我的宗门$',
                    fnc: 'Show_association'
                },
                {
                    reg: '^#加入宗门.*$',
                    fnc: 'Join_association'
                },
                {
                    reg: '^#退出宗门$',
                    fnc: 'Exit_association'
                },
                {
                    reg: '^#宗门(上交|上缴|捐赠)灵石.*$',
                    fnc: 'give_association_lingshi'
                },
                {
                    reg: '^#(升级宗门|宗门升级)$',
                    fnc: 'lvup_association'
                },
                {
                    reg: '^#宗门礼包$',
                    fnc: 'gift_association'
                },
                {
                    reg: '^#宗门捐献记录$',
                    fnc: 'Logs_donate'
                },
                {
                    reg: '任命.*',
                    fnc: 'Set_appointment'
                },
                {
                    reg: '^#(宗门维护|维护宗门)$',
                    fnc: 'Maintenance'
                },
                {
                    reg: '^#(宗门列表)$',
                    fnc: 'List_appointment'
                },
            ]
        })
    }


    async zongmen_test(e) {
        if (!e.isMaster) {
            e.reply("仅限主人操作");
            return;
        }
        e.reply("宗门测试")
        //检索方法
        let msg = "任命外门弟子长老"
        var reg = new RegExp(/长老|外门弟子|内门弟子/);
        let appointment = reg.exec(msg);
        console.log(appointment);
        //let n = this.getContext();
        //console.log("n", n);
        return;
    }


    //判断是否满足创建宗门条件
    async Create_association(e) {
        let usr_qq = e.user_id;
        let ifexistplay = data.existData("player", usr_qq);
        if (!ifexistplay) {
            e.reply("尚未踏入仙途")
            return;
        }
        let player = data.getData("player", usr_qq);
        let now_level_id = data.Level_list.find(item => item.level === player.境界).level_id;
        if (now_level_id < 17) {
            e.reply("修为达到化神再来吧");
            return;
        }
        if (isNotNull(player.宗门)) {
            e.reply("已经有宗门了");
            return;
        }
        if (player.灵石 < 10000) {
            e.reply("开宗立派是需要本钱的,攒到一万灵石再来吧");
            return;
        }

        /** 设置上下文 */
        this.setContext('Get_association_name');
        /** 回复 */
        await e.reply('请发送宗门的名字,一旦设立,无法再改,请慎重取名,(宗门名字最多6个中文字符)', false, { at: true });

        return;
    }

    /** 获取宗门名称 */
    async Get_association_name(e) {
        let usr_qq = e.user_id;
        /** 内容 */
        let new_msg = this.e.message;
        console.log(new_msg[0].type);
        if (new_msg[0].type != "text") {
            this.setContext('Get_association_name');
            await this.reply('请发送文本,请重新输入:');
            return;
        }
        let association_name = new_msg[0].text;
        if (association_name.length > 6) {
            this.setContext('Get_association_name');
            await this.reply('宗门名字最多只能设置6个字符,请重新输入:');
            return;
        }
        var reg = /[^\u4e00-\u9fa5]/g;//汉字检验正则
        var res = reg.test(association_name);
        //res为true表示存在汉字以外的字符
        if (res) {
            this.setContext('Get_association_name');
            await this.reply('宗门名字只能使用中文,请重新输入:');
            return;
        }
        let ifexistass = data.existData("association", association_name);
        if (ifexistass) {
            this.setContext('Get_association_name');
            await this.reply('该宗门已经存在,请重新输入:');
            return;
        }

        console.log("宗门名称获取成功:", association_name);
        //await this.reply('功能还在开发中,敬请期待');
        let now = new Date();
        let nowTime = now.getTime(); //获取当前时间戳
        let date = timestampToTime(nowTime);
        let player = data.getData("player", usr_qq);
        player.宗门 = {
            "宗门名称": association_name,
            "职位": "宗主",
            "time": [date, nowTime]
        }
        await data.setData("player", usr_qq, player)
        await new_Association(association_name, usr_qq);
        await setFileValue(usr_qq, -10000, "灵石")
        await this.reply('宗门创建成功');
        /** 结束上下文 */
        this.finish('Get_association_name');
        //return association_name;
    }


    //宗门显示
    async Show_association(e) {
        let usr_qq = e.user_id;
        let ifexistplay = data.existData("player", usr_qq);
        if (!ifexistplay) { return; }
        let player = data.getData("player", usr_qq);
        if (!isNotNull(player.宗门)) {
            e.reply("尚未加入宗门");
            return;
        }

        let ass = data.getAssociation(player.宗门.宗门名称);
        let msg = [
            `宗门名称: ${ass.宗门名称}` + "\n" +
            `宗门等级: ${ass.宗门等级}` + "\n" +
            `宗门人数: ${ass.所有成员.length}/${宗门人数上限[ass.宗门等级 - 1]}` + "\n" +
            `宗门创立时间: ${ass.创立时间[0]}`
        ];
        let ismt = isNotMaintenance(ass);
        if (ismt) { msg.push("宗门需要维护") }
        else { msg.push("宗门暂不需要维护") };
        msg.push(`灵石池:${ass.灵石池}`);
        let level = data.Level_list.find(item => item.level_id === ass.最低加入境界).level;
        msg.push(`最低加入境界:${level}`);
        msg.push(`宗主:${data.getData("player", ass.宗主).名号}`);
        let ARR = "长老:";
        for (var item in ass.长老) {
            let this_player = data.getData("player", ass.长老[item]);
            ARR = ARR + '\n' + String(this_player.名号);

        }
        msg.push(ARR);
        ARR = "内门弟子:";
        for (var item in ass.内门弟子) {
            let this_player = data.getData("player", ass.内门弟子[item]);
            ARR = ARR + '\n' + String(this_player.名号);
        }
        msg.push(ARR);
        ARR = "外门弟子:";
        for (var item in ass.外门弟子) {
            let this_player = data.getData("player", ass.外门弟子[item]);
            ARR = ARR + '\n' + String(this_player.名号);
        }
        msg.push(ARR);

        await ForwardMsg(e, msg);
        return;
    }


    //宗门礼包
    async gift_association(e) {
        let usr_qq = e.user_id;
        let ifexistplay = data.existData("player", usr_qq);
        if (!ifexistplay) {
            return;
        }
        let player = data.getData("player", usr_qq);
        if (!isNotNull(player.宗门)) {
            e.reply("你尚未加入宗门");
            return;
        }
        let ass = data.getAssociation(player.宗门.宗门名称);
        let ismt = isNotMaintenance(ass);
        if (ismt) {
            e.reply(`宗门尚未维护，快找宗主维护宗门`);
            return;
        }
        let now = new Date();
        let nowTime = now.getTime(); //获取当前日期的时间戳
        let Today = await shijianc(nowTime);
        let lastsign_time = await getLastsign_Asso(usr_qq);//获得上次宗门签到日期
        if (Today.Y == lastsign_time.Y && Today.M == lastsign_time.M && Today.D == lastsign_time.D) {
            e.reply(`今日已经领取过宗门礼包了`);
            return;
        }

        await redis.set("xiuxian:player:" + usr_qq + ":lastsign_Asso_time", nowTime);//redis设置签到时间
        //let now_level_id = data.Level_list.find(item => item.level === player.境界).level_id;
        //给奖励
        let temp = player.宗门.职位;
        let n = 1;
        if (temp == "外门弟子") { n = 1; }
        if (temp == "内门弟子") { n = 2; }
        if (temp == "长老") { n = 3; }
        if (temp == "宗主") { n = 5; }
        let gift_lingshi = ass.宗门等级 * 600 * n;
        let gift_xiuwei = ass.宗门等级 * 400 * n;
        player.灵石 += gift_lingshi;
        player.修为 += gift_xiuwei;
        await data.setData("player", usr_qq, player);
        let msg = [
            segment.at(usr_qq),
            `宗门每日礼包领取成功,获得了${gift_lingshi}灵石,获得了${gift_xiuwei}修为`
        ]
        e.reply(msg);



        //e.reply("还没写好");

        return;
    }


    //加入宗门
    async Join_association(e) {
        let usr_qq = e.user_id;
        let ifexistplay = data.existData("player", usr_qq);
        if (!ifexistplay) {
            return;
        }
        let player = data.getData("player", usr_qq);
        if (isNotNull(player.宗门)) {
            e.reply("你已经有宗门了");
            return;
        }
        let association_name = e.msg.replace("#加入宗门", '');
        association_name = association_name.trim();
        let ifexistass = data.existData("association", association_name);
        if (!ifexistass) {
            e.reply(`宗门不存在`);
            return;
        }
        let ass = data.getAssociation(association_name);
        let player_level_id = data.Level_list.find(item => item.level === player.境界).level_id;
        if (ass.最低加入境界 > player_level_id) {
            let level = data.Level_list.find(item => item.level_id === ass.最低加入境界).level;
            e.reply(`${association_name}招收弟子的最低境界要求为:${level},当前未达到要求`);
            return;
        }
        let mostmem = 宗门人数上限[ass.宗门等级 - 1];//该宗门目前人数上限
        console.log(mostmem);
        let nowmem = ass.所有成员.length;//该宗门目前人数
        console.log(nowmem);
        if (mostmem <= nowmem) {
            e.reply(`${association_name}的弟子人数已经达到目前等级最大,无法加入`);
            return;
        }

        let now = new Date();
        let nowTime = now.getTime(); //获取当前时间戳
        let date = timestampToTime(nowTime);
        player.宗门 = {
            "宗门名称": association_name,
            "职位": "外门弟子",
            "time": [date, nowTime]
        }
        ass.所有成员.push(usr_qq);
        ass.外门弟子.push(usr_qq);
        await data.setData("player", usr_qq, player);
        await data.setAssociation(association_name, ass);
        e.reply(`恭喜你成功加入${association_name}`);
        return;
    }


    //退出宗门
    async Exit_association(e) {
        let usr_qq = e.user_id;
        let ifexistplay = data.existData("player", usr_qq);
        if (!ifexistplay) {
            return;
        }
        let player = data.getData("player", usr_qq);
        if (!isNotNull(player.宗门)) {
            e.reply("你尚未加入宗门");
            return;
        }

        let now = new Date();
        let nowTime = now.getTime(); //获取当前时间戳
        //player.宗门.time[1]  是加入时的时间戳

        console.log(player.宗门)
        let addTime;
        // 判断宗门时间这个变量是旧版还是新版
        if (typeof player.宗门.time == 'undefined') { //旧版本的加入时间是“加入时间”
            addTime = player.宗门.加入时间[1] + 1000 * 60 * 60 * 出入宗门CD;
        } else { //新版本的数据变成了time
            addTime = player.宗门.time[1] + 1000 * 60 * 60 * 出入宗门CD;
        }
        if (addTime > nowTime) {
            e.reply("加入宗门不满" + `${出入宗门CD}小时,无法退出`);
            return;
        }

        if (player.宗门.职位 != "宗主") {
            let ass = data.getAssociation(player.宗门.宗门名称);
            ass[player.宗门.职位] = ass[player.宗门.职位].filter(item => item != usr_qq);
            ass["所有成员"] = ass["所有成员"].filter(item => item != usr_qq);
            await data.setAssociation(ass.宗门名称, ass);
            await delete player.宗门;
            await data.setData("player", usr_qq, player);
            e.reply("退出宗门成功");
        }
        else {
            let ass = data.getAssociation(player.宗门.宗门名称);
            if (ass.所有成员.length < 2) {
                console.log("只有宗主一个人,直接删除宗门");
                fs.rmSync(`${data.filePathMap.association}/${player.宗门.宗门名称}.json`);
                await delete player.宗门;//删除存档里的宗门信息
                await data.setData("player", usr_qq, player);
                e.reply("退出宗门成功,推出后宗门空无一人,自动解散");
            }
            else {
                ass["所有成员"] = ass["所有成员"].filter(item => item != usr_qq);//原来的成员表删掉这个B
                await delete player.宗门;//删除这个B存档里的宗门信息
                await data.setData("player", usr_qq, player);
                //随机一个幸运儿的QQ,优先挑选等级高的
                let randmember_qq;
                if (ass.长老.length > 0) { randmember_qq = await get_random_fromARR(ass.长老); }
                else if (ass.内门弟子.length > 0) { randmember_qq = await get_random_fromARR(ass.内门弟子); }
                else { randmember_qq = await get_random_fromARR(ass.所有成员); }

                let randmember = await data.getData("player", randmember_qq);//获取幸运儿的存档
                ass[randmember.宗门.职位] = ass[randmember.宗门.职位].filter((item) => item != randmember_qq);//原来的职位表删掉这个幸运儿
                ass["宗主"] = randmember_qq;//新的职位表加入这个幸运儿
                randmember.宗门.职位 = "宗主";//成员存档里改职位
                await data.setData("player", randmember_qq, randmember);//记录到存档
                await data.setAssociation(ass.宗门名称, ass);//记录到宗门
                e.reply(`退出宗门成功,退出后,宗主职位由${randmember.名号}接管`);
            }
        }
        return;
    }


    //捐赠灵石
    async give_association_lingshi(e) {
        let usr_qq = e.user_id;
        let ifexistplay = data.existData("player", usr_qq);
        if (!ifexistplay) {
            return;
        }
        let player = data.getData("player", usr_qq);
        if (!isNotNull(player.宗门)) {
            e.reply("你尚未加入宗门");
            return;
        }
        //获取灵石数量
        var reg = new RegExp(/#宗门(上交|上缴|捐赠)灵石/);
        let lingshi = e.msg.replace(reg, '');
        lingshi = lingshi.trim();//去掉空格
        //校验输入灵石数
        if (parseInt(lingshi) == parseInt(lingshi) && parseInt(lingshi) > 0) {
            lingshi = parseInt(lingshi);
        }
        else {
            e.reply("请输入正确灵石数");
            return;
        }
        if (player.灵石 < lingshi) {
            e.reply(`你身上只有${player.灵石}灵石,数量不足`);
            return;
        }
        let ass = data.getAssociation(player.宗门.宗门名称);

        if (ass.灵石池 + lingshi > 宗门灵石池上限[ass.宗门等级 - 1]) {
            e.reply(`${ass.宗门名称}的灵石池最多还能容纳${宗门灵石池上限[ass.宗门等级 - 1] - ass.灵石池}灵石,请重新捐赠`);
            return;
        }
        ass.灵石池 += lingshi;
        if (!isNotNull(player.宗门.lingshi_donate)) {
            player.宗门.lingshi_donate = 0;//未定义捐赠数量则为0
        }
        player.宗门.lingshi_donate += lingshi;
        await data.setData("player", usr_qq, player);
        await data.setAssociation(ass.宗门名称, ass);
        await setFileValue(usr_qq, -lingshi, "灵石");
        e.reply(`捐赠成功,你身上还有${player.灵石 - lingshi}灵石,宗门灵石池目前有${ass.灵石池}灵石`);
        return;
    }


    //宗门捐献记录
    async Logs_donate(e) {
        let usr_qq = e.user_id;
        let ifexistplay = data.existData("player", usr_qq);
        if (!ifexistplay) {
            return;
        }
        let player = data.getData("player", usr_qq);
        if (!isNotNull(player.宗门)) {
            e.reply("你尚未加入宗门");
            return;
        }
        let ass = data.getAssociation(player.宗门.宗门名称);
        let donate_list = [];
        for (let i in ass.所有成员) {//遍历所有成员
            let member_qq = ass.所有成员[i];
            let member_data = data.getData("player", member_qq);
            if (!isNotNull(member_data.宗门.lingshi_donate)) {
                member_data.宗门.lingshi_donate = 0;//未定义捐赠数量则为0
            }
            donate_list[i] = {
                "name": member_data.名号,
                "lingshi_donate": member_data.宗门.lingshi_donate,
            }
        }
        donate_list.sort(sortBy("lingshi_donate"));
        let msg = [`${ass.宗门名称} 灵石捐献记录表`];
        for (let i = 0; i < donate_list.length; i++) {
            msg.push(`第${i + 1}名  ${donate_list[i].name}  捐赠灵石:${donate_list[i].lingshi_donate}`,);
        }
        await ForwardMsg(e, msg);

        return;
    }


    //升级宗门
    async lvup_association(e) {
        let usr_qq = e.user_id;
        let ifexistplay = data.existData("player", usr_qq);
        if (!ifexistplay) {
            return;
        }
        let player = data.getData("player", usr_qq);
        if (!isNotNull(player.宗门)) {
            e.reply("你尚未加入宗门");
            return;
        }
        if (player.宗门.职位 != "宗主") {
            e.reply("只有宗主可以操作");
            return;
        }
        let ass = data.getAssociation(player.宗门.宗门名称);
        if (ass.宗门等级 == 宗门人数上限.length) {
            e.reply("已经是最高等级宗门");
            return;
        }
        if (ass.灵石池 < ass.宗门等级 * 10000) {
            e.reply(`本宗门目前灵石池中仅有${ass.灵石池}灵石,当前宗门升级需要${ass.宗门等级 * 10000}灵石,数量不足`);
            return;
        }
        ass.灵石池 -= ass.宗门等级 * 10000;
        ass.宗门等级 += 1;
        await data.setAssociation(ass.宗门名称, ass);
        e.reply("宗门升级成功" + `当前宗门等级为${ass.宗门等级},宗门人数上限提高到:${宗门人数上限[ass.宗门等级 - 1]}`);
        return;
    }


    //任命职位
    async Set_appointment(e) {
        let usr_qq = e.user_id;
        let isat = e.message.some((item) => item.type === "at");
        console.log("有无at信息", isat);
        if (!isat) { return; }//没有at信息直接返回,不执行
        let ifexistplay = data.existData("player", usr_qq);
        if (!ifexistplay) {
            return;
        }
        let player = await data.getData("player", usr_qq);
        if (!isNotNull(player.宗门)) {
            e.reply("你尚未加入宗门");
            return;
        }
        if (player.宗门.职位 != "宗主") {
            e.reply("只有宗主可以操作");
            return;
        }

        let atItem = e.message.filter((item) => item.type === "at");//获取at信息
        let member_qq = atItem[0].qq;
        if (usr_qq == member_qq) { e.reply("???"); return; }//at宗主自己,这不扯犊子呢

        let ass = await data.getAssociation(player.宗门.宗门名称);
        console.log(player.宗门.宗门名称, ass.所有成员);
        let isinass = ass.所有成员.some((item) => item == member_qq);//这个命名可太糟糕了
        if (!isinass) { e.reply("只能设置宗门内弟子的职位"); return; }
        let member = data.getData("player", member_qq);//获取这个B的存档
        let now_apmt = member.宗门.职位;//这个B现在的职位
        let full_apmt = ass.所有成员.length;
        //检索输入的第一个职位
        var reg = new RegExp(/长老|外门弟子|内门弟子/);
        let appointment = reg.exec(e.msg);//获取输入的职位
        if (appointment == now_apmt) { e.reply(`此人已经是本宗门的${appointment}`); return; }
        if (appointment == "长老") {
            full_apmt = 长老人数上限[ass.宗门等级 - 1];
        }
        else if (appointment == "内门弟子") {
            full_apmt = 内门弟子上限[ass.宗门等级 - 1];
        }
        if (ass[appointment].length >= full_apmt) {
            e.reply(`本宗门的${appointment}人数已经达到上限`); return;
        }
        member.宗门.职位 = appointment;//成员存档里改职位
        ass[now_apmt] = ass[now_apmt].filter((item) => item != member_qq);//原来的职位表删掉这个B
        ass[appointment].push(member_qq);//新的职位表加入这个B
        await data.setData("player", member_qq, member);//记录到存档
        await data.setAssociation(ass.宗门名称, ass);//记录到宗门
        e.reply([segment.at(member_qq), `${ass.宗门名称} 宗主已经成功将${member.名号}任命为${appointment}!`]);
        return;
    }


    //宗门维护
    async Maintenance(e) {
        let usr_qq = e.user_id;
        let ifexistplay = data.existData("player", usr_qq);
        if (!ifexistplay) { return; }
        let player = await data.getData("player", usr_qq);
        if (!isNotNull(player.宗门)) {
            e.reply("你尚未加入宗门");
            return;
        }
        if (player.宗门.职位 != "宗主") {
            e.reply("只有宗主可以操作");
            return;
        }
        let ass = await data.getAssociation(player.宗门.宗门名称);
        let now = new Date();
        let nowTime = now.getTime(); //获取当前日期的时间戳
        let nextmt_time = await shijianc(ass.维护时间 + 1000 * 60 * 60 * 24 * 7);//获得下次宗门维护日期,7天后
        if (ass.维护时间 > nowTime - 1000 * 60 * 60 * 24 * 7) {
            e.reply(`当前无需维护,下次维护时间:${nextmt_time.Y}年${nextmt_time.M}月${nextmt_time.D}日${nextmt_time.h}时${nextmt_time.m}分${nextmt_time.s}秒`);
            return;
        }
        if (ass.灵石池 < ass.宗门等级 * 100000) {
            e.reply(`目前宗门维护需要${ass.宗门等级 * 100000}灵石,本宗门灵石池储量不足`);
            return;
        }
        ass.灵石池 -= ass.宗门等级 * 100000;
        ass.维护时间 = nowTime;
        await data.setAssociation(ass.宗门名称, ass);//记录到宗门
        nextmt_time = await shijianc(ass.维护时间 + 1000 * 60 * 60 * 24 * 7);
        e.reply(`宗门维护成功,下次维护时间:${nextmt_time.Y}年${nextmt_time.M}月${nextmt_time.D}日${nextmt_time.h}时${nextmt_time.m}分${nextmt_time.s}秒`);
        return;
    }


    //宗门列表
    async List_appointment(e) {
        let usr_qq = e.user_id;
        let ifexistplay = data.existData("player", usr_qq);
        if (!ifexistplay) { return; }
        let dir = data.filePathMap.association;
        let File = fs.readdirSync(dir);
        File = File.filter(file => file.endsWith(".json"));//这个数组内容是所有的宗门名称

        let temp = ["宗门列表"];
        if (File.length == 0) {
            temp.push("暂时没有宗门数据");
        }
        for (var i = 0; i < File.length; i++) {
            let this_name = File[i].replace(".json", '');
            let this_ass = await data.getAssociation(this_name);
            temp.push(`序号:${1 + i} ` + '\n' + `宗门名称: ${this_ass.宗门名称}` + '\n' + `人数: ${this_ass.所有成员.length}/${宗门人数上限[this_ass.宗门等级 - 1]}` +
                "\n" + `宗门等级: ${this_ass.宗门等级}` + '\n' +
                `宗主qq: ${this_ass.宗主}`
            );
        }
        await ForwardMsg(e, temp);
        return;
    }

}
/**————————————————————————————————————————————————————分割线—————————————————————————————————————————————————————————*/


/**
 * 创立新的宗门
 * @param name 宗门名称
 * @param holder_qq 宗主qq号
 */
async function new_Association(name, holder_qq) {
    let now = new Date();
    let nowTime = now.getTime(); //获取当前时间戳
    let date = timestampToTime(nowTime);
    console.log("开始")
    let Association = {
        "宗门名称": name,
        "宗门等级": 1,
        "创立时间": [date, nowTime],
        "灵石池": 0,
        "宗主": holder_qq,
        "长老": [],
        "内门弟子": [],
        "外门弟子": [],
        "所有成员": [holder_qq,],
        "药园": {
            "药园等级": 1,
            "作物": [
                {
                    "name": "凝血草",
                    "start_time": nowTime,
                    "who_plant": holder_qq
                }
            ],
        },
        "维护时间": nowTime,
        "大阵血量": 114514,
        "最低加入境界": 1,

    }
    data.setAssociation(name, Association);
    //let ass = data.getAssociation(holder_qq);
    console.log("写入成功")
    return;
}


/**
 * 增加player文件某属性的值（在原本的基础上增加）
 * @param user_qq
 * @param num 属性的value
 * @param type 修改的属性
 * @returns {Promise<void>}
 */
export async function setFileValue(user_qq, num, type) {
    let user_data = data.getData("player", user_qq);
    let current_num = user_data[type];//当前灵石数量
    let new_num = current_num + num;
    if (type == "当前血量" && new_num > user_data.血量上限) {
        new_num = user_data.血量上限;//治疗血量需要判读上限
    }
    user_data[type] = new_num;
    await data.setData("player", user_qq, user_data);
    return;
}



/**
 * 判断宗门是否需要维护
 * @param ass 宗门对象
 * @returns true or false
 */
function isNotMaintenance(ass) {
    let now = new Date();
    let nowTime = now.getTime(); //获取当前日期的时间戳
    if (ass.维护时间 > nowTime - 1000 * 60 * 60 * 24 * 7) {
        return false;
    }
    return true;
}

/**
 * 判断对象是否不为undefined且不为null
 * @param obj 对象
 * @returns obj==null/undefined,return false,other return true
 */
function isNotNull(obj) {
    if (obj == undefined || obj == null)
        return false;
    return true;
}

//对象数组排序
function sortBy(field) {//从大到小,b和a反一下就是从小到大
    return function (b, a) {
        return a[field] - b[field];
    }
}
// function isNotBlank(value) {
//     if (value ?? '' !== '') {
//         return true;
//     } else {
//         return false;
//     }
// }



//获取上次签到时间
async function getLastsign_Asso(usr_qq) {
    //查询redis中的人物动作
    let time = await redis.get("xiuxian:player:" + usr_qq + ":lastsign_Asso_time");
    console.log(time);
    if (time != null) {
        let data = await shijianc(parseInt(time))
        return data;
    }
    return false;
}


